package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.sellerquery2.parameter.GetOrderSalesRefundFlowsBean;
import com.ymatou.iapi.trading.sellerquery2.parameter.enums.SalesRefundStatusEnum;
import com.ymatou.iapi.trading.sellerquery.parameter.resp.GetOrderSalesRefundFlowsResp;
import com.ymatou.query.seller.trading.api.parameter.Flows;
import com.ymatou.query.seller.trading.api.service.OrderCreate;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 获取订单退货退款信息 FreeMaker Template自动生成代码
 */
@SuppressWarnings("rawtypes")
@TestSuite
public class Ts_GetOrderSalesRefundFlows {
	private static GetOrderSalesRefundFlowsBean getordersalesrefundflowsBean;
//	private static com.ymatou.query.seller.trading.api.service.GetOrderSalesRefundFlowsCall getordersalesrefundflowsCall;
	private static com.ymatou.iapi.trading.sellerquery2.service.GetOrderSalesRefundFlowsCall getordersalesrefundflowsCall;
	private static Flows order;
	private static Flows refund;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取订单退货退款信息");
	}

	@Before
	public void caseUp() {
		getordersalesrefundflowsBean = new GetOrderSalesRefundFlowsBean();
//		getordersalesrefundflowsCall = new com.ymatou.query.seller.trading.api.service.GetOrderSalesRefundFlowsCall(); 
		getordersalesrefundflowsCall = new com.ymatou.iapi.trading.sellerquery2.service.GetOrderSalesRefundFlowsCall();
	}
	
	private void verify(){
		GetOrderSalesRefundFlowsResp expectResponse = new GetOrderSalesRefundFlowsResp();
		ConstructExpectedResult(expectResponse);
		GetOrderSalesRefundFlowsResp actualResponse = getordersalesrefundflowsCall.getOrderSalesRefundFlowsResp();
		Flows expOrder = expectResponse.getOrder();
		Flows actOrder = actualResponse.getOrder();
		if(expOrder == null){
			DataUtil.verify(null, actOrder, "验证order==null");
		}else{
			DataUtil.verify(expOrder.getCash(), actOrder.getCash(),"验证order.Cash");
			DataUtil.verify(expOrder.getCoupon(), actOrder.getCoupon(),"验证order.Coupon");
			DataUtil.verify(expOrder.getFreight(), actOrder.getFreight(),"验证order.Freight");
			DataUtil.verify(expOrder.getGift(), actOrder.getGift(),"验证order.Gift");
			DataUtil.verify(expOrder.getSellerCoupon(), DataUtil.BigDecimall.GetValueOrDefault(actOrder.getSellerCoupon()),"验证order.SellerCoupon");
			DataUtil.verify(expOrder.getTotal(), actOrder.getTotal(),"验证order.Total");
		}
		Flows expRefund = expectResponse.getRefund();
		Flows actRefund = actualResponse.getRefund();
		if(expRefund == null){
			DataUtil.verify(null, actRefund, "验证order==null");
		}else{
			DataUtil.verify(expRefund.getCash(), actRefund.getCash(),"验证order.Cash");
			DataUtil.verify(expRefund.getCoupon(), actRefund.getCoupon(),"验证order.Coupon");
			DataUtil.verify(expRefund.getFreight(), actRefund.getFreight(),"验证order.Freight");
			DataUtil.verify(expRefund.getGift(), actRefund.getGift(),"验证order.Gift");
			DataUtil.verify(expRefund.getSellerCoupon(), actRefund.getSellerCoupon(),"验证order.SellerCoupon");
			DataUtil.verify(expRefund.getTotal(), actRefund.getTotal(),"验证order.Total");
		}
	}
	
	private void ConstructExpectedResult(GetOrderSalesRefundFlowsResp response){
		execute(DataUtil.Integerr.GetValueOrDefault(getordersalesrefundflowsBean.getOrderId()), 
				getordersalesrefundflowsBean.getStatus());
		response.setOrder(order);
		response.setRefund(refund);
	}
	
	private Flows ConvertResult(List<Map> list){
		Flows o = null;
		if(false == DataUtil.isNullOrEmpty(list)){
			o = new Flows();
			DataUtil.mapToBean(o, list.get(0));
		}
		return o;
	}
	
	private void execute(int orderId,List<Integer> status){
		if(orderId != 0){
			DefaultWapper db = new DefaultWapper();
			List<Map> list1 = db.executeSql(getOrderSalesRefundFlows1(orderId, status));
			List<Map> list2 = db.executeSql(getOrderSalesRefundFlows2(orderId, status));
			order = ConvertResult(list1);
			refund = ConvertResult(list2);
		}
	}
	
	public static String getOrderSalesRefundFlows1(int orderId,List<Integer> status){
		StringBuilder sb = new StringBuilder();
		String var1 = "declare @total decimal(18,2)";
		String var2 = "declare @freight decimal(18,2)";
		String sql1 = "SELECT"
				+"	@total = ("
				+"		CASE bShangouOrder"
				+"		WHEN 1 THEN"
				+"			fTotalPrice"
				+"		ELSE"
				+"			fOrderPrice"
				+"		END"
				+"	) ,@freight = fFreight"
				+" FROM"
				+"	Ymt_Orders (nolock)"
				+"WHERE"
				+"	iOrderId = "+orderId;
		if(false == DataUtil.isNullOrEmpty(status)){
				sql1 += " AND iSalesRefundStatus IN "+DataUtil.toInOfSql(status);
		}
		String sql2 = " SELECT"
				+" 	@total AS Total,"
				+" 	fPaidAmountOfCash + fPostPaidAmountOfCash AS Cash,"
				+" 	fPaidAmountOfGift + fPostPaidAmountOfGift AS Gift,"
				+" 	fPaidAmountOfCoupon + fPostPadiAmountOfCoupon AS Coupon,"
				+" 	fDiscountOfThirdParty AS ThirdPartyDiscount,"
				+" 	@freight AS Freight"
				+" FROM"
				+" 	Ymt_OrderState (nolock)"
				+" WHERE"
				+" 	@total IS NOT NULL"
				+" AND iOrderId = "+orderId;
		sb.append(var1);
		sb.append("; \n");
		sb.append(var2);
		sb.append("; \n");
		sb.append(sql1);
		sb.append("; \n");
		sb.append(sql2);
		return sb.toString();
	}
	
	public static String getOrderSalesRefundFlows2(int orderId,List<Integer> status){
		StringBuilder sb = new StringBuilder();
		String var1 = "declare @total decimal(18,2)";
		String var2 = "declare @freight decimal(18,2)";
		String sql1 = "SELECT"
				+"	@total = ("
				+"		CASE bShangouOrder"
				+"		WHEN 1 THEN"
				+"			fTotalPrice"
				+"		ELSE"
				+"			fOrderPrice"
				+"		END"
				+"	) ,@freight = fFreight"
				+" FROM"
				+"	Ymt_Orders (nolock)"
				+"WHERE"
				+"	iOrderId = "+orderId;
		if(false == DataUtil.isNullOrEmpty(status)){
				sql1 += " AND iSalesRefundStatus IN "+DataUtil.toInOfSql(status);
		}
		String sql2 = " if @total is not null begin"
				+" 	select RefundAmount as Total, "
				+" 	RefundedAmountOfCash as Cash,"
				+" 	RefundedAmountOfGift as Gift,"
				+"     RefundedDiscountOfThirdParty as ThirdPartyDiscount,"
				+" 	0 as Coupon "
				+" 	from Ymt_RefundBill(nolock) where OrderId = "+orderId
				+" end";
		sb.append(var1);
		sb.append("; \n");
		sb.append(var2);
		sb.append("; \n");
		sb.append(sql1);
		sb.append("; \n");
		sb.append(sql2);
		return sb.toString();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getordersalesrefundflowsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}


	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetOrderSalesRefundFlows_001() {
		Logger.start(true, "一个订单,多次退货退款,获取订单退货退款信息");
		try {
			Logger.comment("准备测试数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			o.setProductCount(3);
			int orderId = o.create4_RefundOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备测试数据完成");
			List<Integer> refundStatusList = new ArrayList<Integer>();
			refundStatusList.add(SalesRefundStatusEnum.Success.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Fail.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Processing.getCode());
			getordersalesrefundflowsBean.setOrderId(orderId);
			getordersalesrefundflowsBean.setStatus(refundStatusList);
			getordersalesrefundflowsCall.setData(getordersalesrefundflowsBean);
			getordersalesrefundflowsCall.callService();

			DataUtil.verifyEquals(ECode.SUCCESS,
					getordersalesrefundflowsCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_GetOrderSalesRefundFlows_002() {
		Logger.start(true, "一个订单,多次退货退款,订单退款状态为null,获取订单退货退款信息");
		try {
			Logger.comment("准备测试数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			o.setProductCount(3);
			int orderId = o.create4_RefundOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备测试数据完成");
			getordersalesrefundflowsBean.setOrderId(orderId);
			getordersalesrefundflowsBean.setStatus(null);
			getordersalesrefundflowsCall.setData(getordersalesrefundflowsBean);
			getordersalesrefundflowsCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					getordersalesrefundflowsCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetOrderSalesRefundFlows_003() {
		Logger.start(true, "该订单没有退款记录,获取订单退货退款信息");
		try {
			Logger.comment("准备测试数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			o.setProductCount(3);
			int orderId = o.create3_ShippedOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备测试数据完成");
			List<Integer> refundStatusList = new ArrayList<Integer>();
			refundStatusList.add(SalesRefundStatusEnum.Success.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Fail.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Processing.getCode());

			getordersalesrefundflowsBean.setOrderId(orderId);
			getordersalesrefundflowsBean.setStatus(refundStatusList);
			getordersalesrefundflowsCall.setData(getordersalesrefundflowsBean);
			getordersalesrefundflowsCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					getordersalesrefundflowsCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetOrderSalesRefundFlows_004() {
		Logger.start(true, "订单号不存在,获取订单退货退款信息");
		try {
			int orderId = 404;

			List<Integer> refundStatusList = new ArrayList<>();
			refundStatusList.add(SalesRefundStatusEnum.Success.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Fail.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Processing.getCode());

			getordersalesrefundflowsBean.setOrderId(orderId);
			getordersalesrefundflowsBean.setStatus(refundStatusList);
			getordersalesrefundflowsCall.setData(getordersalesrefundflowsBean);
			getordersalesrefundflowsCall.callService();


			Logger.verifyEquals(ECode.SUCCESS,
					getordersalesrefundflowsCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetOrderSalesRefundFlows_005() {
		Logger.start(true, "订单号为null,获取订单退货退款信息");
		try {

			List<Integer> refundStatusList = new ArrayList<Integer>();
			refundStatusList.add(SalesRefundStatusEnum.Success.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Fail.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Processing.getCode());

			getordersalesrefundflowsBean.setStatus(refundStatusList);
			getordersalesrefundflowsCall.setData(getordersalesrefundflowsBean);
			getordersalesrefundflowsCall.callService();

			Logger.verifyEquals(ECode.ERROR400,
					getordersalesrefundflowsCall.getCode(), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_GetOrderSalesRefundFlows_006() {
		Logger.start(true, "订单退款失败,获取订单退货退款信息");
		try {
			Logger.comment("准备测试数据");
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			OrderCreate o = new OrderCreate();
			int orderId = o.create4_RefundFailedOrder(userId, sellerId).getiOrderId();
			Thread.sleep(2000l);
			Logger.comment("准备测试数据完成");
			List<Integer> refundStatusList = new ArrayList<Integer>();
			refundStatusList.add(SalesRefundStatusEnum.Success.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Fail.getCode());
			refundStatusList.add(SalesRefundStatusEnum.Processing.getCode());
			getordersalesrefundflowsBean.setOrderId(orderId);
			getordersalesrefundflowsBean.setStatus(refundStatusList);
			getordersalesrefundflowsCall.setData(getordersalesrefundflowsBean);
			getordersalesrefundflowsCall.callService();

			DataUtil.verifyEquals(ECode.SUCCESS,
					getordersalesrefundflowsCall.getCode(), "验证code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}